<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-08-16 13:38:04
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;

use App\Repositories\ProDyecraftRepository;
use App\Repositories\ProDyecraftGroupRepository;
use App\Http\Services\Service;
use Illuminate\Support\Facades\DB;

/**
 * @property ProDyecraftGroupRepository $group
 */
class DyecraftService extends Service
    {
    public function boot(ProDyecraftRepository $repository, ProDyecraftGroupRepository $group)
        {
        $this->repository = $repository;
        $this->group      = $group;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }
    protected function enable()
        {
        $data = $this->repository->whereIn("id", $this->getRequestIds())->first();
        if ($data->status_id == 1)
            fail("单据【{$data->code}】已启用！");
        return auditSave($data);
        }

    protected function disable()
        {
        $data = $this->repository->whereIn("id", $this->getRequestIds())->first();
        if ($data->status_id == 0)
            fail("单据【{$data->code}】未启用！");
        return auditSave($data, false);
        }
    protected function save()
        {
        // DB::enableQueryLog();
        // dd(DB::getQueryLog());
        $data    = request()->toArray();
        $datails = $data['details'];
        unset($data['details']);

        $endscraft_codes = array_map(
            function ($detail)
                {
                return $this->group->firstOrCreate($detail)->endscraft_code;
                },
            array_map(function ($item) use ($data)
                {
                return [
                    'dyescraft_code' => $data['code'],
                    'endscraft_code' => $item['code'],
                ];
                }, $datails)
        );
        $this->group
            ->whereDyescraft_code($data['code'])
            ->whereNotIn("endscraft_code", $endscraft_codes)
            ->get()
            ->filter(function ($group)
                {
                $group->delete();
                });

        return $this->repository->autosave($data);
        }
    protected function delete($request)
        {
        return $this->repository->delete($this->getRequestIds());
        }
    protected function sync()
        {
        $data = app("ZhiJing")->Craft->query('', '', 1);

        $this->repository->update(['craftid' => '']);

        foreach ($data['data'] as $v) {

            $gdata = $this->repository->whereCode($v['code'])->first();

            if ($gdata) {
                $gdata->craftid = $v['id'];
                $gdata->name    = $v['name'];
                $gdata->save();
                }
            else {

                $materialCategoryIds = substr(trim($v['code']), 0, 1);
                $materialCategoryIds = !in_array($materialCategoryIds, ['B', 'Z', 'S', 'F']) ? "" : $materialCategoryIds;

                $this->repository->autosave([
                    'craftid'             => $v['id'],
                    'name'                => $v['name'],
                    'code'                => $v['code'],
                    'materialCategoryIds' => $materialCategoryIds
                ]);
                }
            }

        $this->repository->whereCraftid('')->delete();
        return;
        }
    public function details()
        {
        $endscraft_code = $this->group->whereDyescraft_code(request("code"))->pluck('endscraft_code');
        return $this->repository->whereIn("code", $endscraft_code)->get();
        }
    }